package com.yang.Offer;

public class Offer18 {
}
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
// 删除链表节点
class Offer18Solution {
    public SolutionOffer06.ListNode deleteNode(SolutionOffer06.ListNode head, int val) {
        if(head == null){
            return head;
        }
        if(head.val == val){
            return head.next;
        }

        SolutionOffer06.ListNode pre = head;
        SolutionOffer06.ListNode cur = head.next;
        while(cur != null){
            if(cur.val == val){
                if(cur.next != null){
                    pre.next = cur.next;
                    return head;
                }
                else{
                    pre.next = null;
                    return head;
                }
            }
            pre = cur;
            cur = cur.next;
        }
        return head;
    }
}
